package com.wjjun.basemvpvm.view;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.blankj.utilcode.util.KeyboardUtils;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.wjjun.basemvpvm.BR;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;


/**
 * author Wjjun
 * Created 2018/3/6.
 */

public abstract class DataBindingActivity<VM extends BaseViewModel, VDB extends ViewDataBinding> extends RxAppCompatActivity {

    private View rootView;
    protected VDB viewDataBinding;
    protected VM viewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBinding();
        viewModel = initViewModel();
        bindViewModel();
    }

    protected void initBinding() {
        int rootLayoutId = getLayoutId();
        if (rootLayoutId > 0) {
            viewDataBinding = DataBindingUtil.setContentView(this, rootLayoutId);
        }
//        viewDataBinding = DataBindingUtil.inflate(getLayoutInflater(), rootLayoutId, null, false);
//        if (viewDataBinding == null) {
//            rootView = getLayoutInflater().inflate(rootLayoutId, null, false);
//        } else {
//            rootView = viewDataBinding.getRoot();
//        }
    }

    /**
     * 获取布局文件id
     */
    public abstract int getLayoutId();

    /**
     * 初始化ViewModel
     */
    protected abstract VM initViewModel();

    /**
     * 绑定ViewModel
     */
    protected void bindViewModel() {
        //绑定presenter
        viewDataBinding.setVariable(BR.presenter, this);
        //绑定viewModel
        viewDataBinding.setVariable(BR.viewModel, viewModel);
    }

    @Override
    protected void onPause() {
        super.onPause();
        KeyboardUtils.hideSoftInput(this);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

}
